package com.zhentao.service.impl;

import com.zhentao.minio.MinioConfig;
import com.zhentao.minio.MinioConfigProperties;
import com.zhentao.service.FileStorageService;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import lombok.SneakyThrows;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;

@EnableConfigurationProperties(MinioConfigProperties.class)
@Import(MinioConfig.class)
@Service
public class FileStorageServiceImpl implements FileStorageService {
    @Resource
    private MinioClient minioClient;
    @Resource
    private MinioConfigProperties minioConfigProperties;
    @SneakyThrows
    @Override
    public String uploadImage(String fileName, InputStream inputStream) {

        PutObjectArgs build = PutObjectArgs.builder()
                .bucket(minioConfigProperties.getBucket())
                .object(fileName)
                .stream(inputStream, inputStream.available(), -1)
//                .contentType("image/jpg")
                .build();

        ObjectWriteResponse response = minioClient.putObject(build);

        String path = minioClient.getObjectUrl(build.bucket(), build.object());

        return path;
    }
}
